<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
    <title>新用户注册</title>
    <!-- 引入 WeUI -->
    <link rel="stylesheet" href="https://res.wx.qq.com/open/libs/weui/1.1.2/weui.min.css"/>
    <script type="text/javascript" src="https://res.wx.qq.com/open/libs/weuijs/1.1.3/weui.min.js"></script>
    <script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <script type="text/javascript" src="/config.js"></script>
</head>
<body>
<form id="userRegister" style="padding: 10px;">
    <div class="weui-cells__title">用户信息填写</div>
    <div class="weui-cells weui-cells_form">
        <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">姓名</label></div>
            <div class="weui-cell__bd">
                <input id="name" class="weui-input" type="text" pattern="[0-9]*" placeholder="请输入姓名"/>
            </div>
        </div>
        <div class="weui-cell weui-cell_vcode">
            <div class="weui-cell__hd">
                <label class="weui-label">手机号<span style="color: red"> * </span> </label>
            </div>
            <div class="weui-cell__bd">
                <input id="mobile" class="weui-input" type="tel" placeholder="请输入手机号">
            </div>
            <div class="weui-cell__ft">
                <a id="getVerifyCode" href="#" class="weui-vcode-btn">获取验证码</a>
            </div>
        </div>
        <div class="weui-cell weui-cell_vcode">
            <div class="weui-cell__hd"><label class="weui-label">验证码<span style="color: red"> * </span></label></div>
            <div class="weui-cell__bd">
                <input id="verifyCode" class="weui-input" type="number" placeholder="请输入验证码"/>
            </div>
            <div class="weui-cell__ft">
                <img class="weui-vcode-img"/>
            </div>
        </div>
        <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">邮寄地址<span style="color: red"> * </span></label></div>
            <div class="weui-cell__bd">
                <textarea style="vertical-align: middle" id="address" class="weui-textarea" placeholder="请输入详细地址" rows="2"></textarea>
                <div class="weui-textarea-counter"><span>0</span>/100</div>
            </div>
        </div>
    </div>

    <label for="weuiAgree" class="weui-agree">
        <input checked id="weuiAgree" type="checkbox" class="weui-agree__checkbox">
        <span class="weui-agree__text">阅读并同意<a href="javascript:void(0);">《相关条款》</a></span>
    </label>

    <div class="weui-btn-area">
        <a id="submit" class="weui-btn weui-btn_primary" href="#" id="showTooltips">确定</a>
    </div>

    <script type="text/javascript">

        function getQueryString(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]);
            return null;
        }

        $("#submit").click(function () {

            if ($("#mobile").val() == '') {
                weui.topTips('请填写手机号');
                return;
            }
            if ($("#verifyCode").val() == '') {
                weui.topTips('请填写手机验证码');
                return;
            }
            if ($("#address").val() == '') {
                weui.topTips('请填写邮寄地址');
                return;
            }
            weui.confirm('请确认填写的信息', {
                title: '消息提示',
                buttons: [{
                    label: '是',
                    type: 'primary',
                    onClick: function () {
                        $.ajax({
                            type: 'get',
                            dataType: 'json',
                            url: appConfig.appTestDomain + '/user/saveUser.do',
                            data: {
                                state: getQueryString("state"),
                                code: getQueryString("code"),
                                name: $("#name").val(),
                                mobile: $("#mobile").val(),
                                verifyCode: $("#verifyCode").val(),
                                address: $("#address").val()
                            },
                            success: function (data) {
                                console.info("success : " + JSON.stringify(data));
                                if (data && data.success == false) {
                                    weui.topTips(data.msgInfo);
                                    window.location.href = appConfig.appTestDomain + "/user/newUserAward.html";
                                } else {
                                    window.location.href = appConfig.appTestDomain + "/user/newUserAward.html";
                                }
                            },
                            timeout: 15000,
                            error: function (data) {
                                console.info("error : " + data);
                                if (data) {
                                    weui.topTips(data.msgInfo);
                                }
                            }
                        });
                    }
                }, {
                    label: '否',
                    type: 'default',
                    onClick: function () {
                    }
                }]
            });
        });

        $("#getVerifyCode").click(function () {
            if ($("#mobile").val() == '') {
                weui.topTips('请填写手机号');
                return;
            }
            $.ajax({
                type: 'get',
                dataType: 'json',
                url: appConfig.appTestDomain + '/user/verifyCode.do',
                data: {
                    mobile: $("#mobile").val(),
                    code: getQueryString("code"),
                },
                success: function (data) {
                    if (data && data.success == false) {
                        weui.topTips(data.msgInfo);
                    } else {
                        weui.topTips('发送成功', 1000);
                    }
                },
                timeout: 15000,
                error: function (data) {
                    console.log(data);
                    weui.alert(data.msgInfo);
                }
            });
        });
    </script>
</form>
</body>
</html>